package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityRecDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class RecDetailActivity extends BaseAc<ActivityRecDetailBinding> {

    public static StkResBean detail;
    private boolean isCollect;
    private int num;
    private List<StkResBean> listSp;

    @Override
    protected int onCreate() {
        return R.layout.activity_rec_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecDetailBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.ivShare.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCollect:
                if (isCollect) {
                    listSp.remove(detail);
                    AppSpUtils.saveCollectList(listSp);
                    isCollect = false;
                    num--;
                    mDataBinding.tvCollect.setText(getString(R.string.collect_num, num));
                    mDataBinding.ivCollect.setImageResource(R.drawable.shoucang1);
                    mDataBinding.ivCollectSmall.setImageResource(R.drawable.sc1);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(detail);
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                    isCollect = true;
                    num++;
                    mDataBinding.tvCollect.setText(getString(R.string.collect_num, num));
                    mDataBinding.ivCollect.setImageResource(R.drawable.shoucang2);
                    mDataBinding.ivCollectSmall.setImageResource(R.drawable.sc2);
                }
                break;
            case R.id.ivShare:
                IntentUtil.shareText(mContext, detail.getUrl());
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        mDataBinding.tvRecDetailTitle.setText(detail.getName());
        mDataBinding.webView.loadUrl(detail.getUrl());
        Random random = new Random();
        num = 1000 + random.nextInt(1000);
        int num1 = random.nextInt(9);
        int num2 = random.nextInt(9);
        mDataBinding.tvHot.setText(getString(R.string.hot_text, num1, num2));
        mDataBinding.tvCollect.setText(getString(R.string.collect_num, num));
        listSp = AppSpUtils.getCollectList();
        isCollect = false;
        mDataBinding.ivCollect.setImageResource(R.drawable.shoucang1);
        mDataBinding.ivCollectSmall.setImageResource(R.drawable.sc1);
        if (listSp != null && listSp.size() != 0) {
            for (StkResBean bean : listSp) {
                if (bean.equals(detail)) {
                    isCollect = true;
                    mDataBinding.ivCollect.setImageResource(R.drawable.shoucang2);
                    mDataBinding.ivCollectSmall.setImageResource(R.drawable.sc2);
                    break;
                }
            }
        }
    }

}